#include "util.hpp"
#include "db.hpp"
#include "online.hpp"
#include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"
#include "server.hpp"
#define HOST "127.0.0.1"
#define PORT 3306
#define USER "root"
#define PASS "123456"
#define DBNAME "gobang"

void test_mysql()
{
    MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASS, DBNAME, PORT);
    const char *sql = "insert stu values(null,'小黑子',18,53,68,87);";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if (ret == false)
    {
        return;
    }
    mysql_util::mysql_destory(mysql);
}
void test_json()
{
    // 1.将需要进行序列化的数据，存储在Json::value 对象中
    Json::Value root;
    root["姓名"] = "小明";
    root["年龄"] = 18;
    root["成绩"].append(98);
    root["成绩"].append(88.5);
    root["成绩"].append(78.5);
    std::string str;
    bool ret = json_util::serialize(root, str);
    if (!ret)
    {
        std::cout << "serialize failed!!!" << std::endl;
    }
    std::cout << str << std::endl;

    Json::Value root1;
    ret = json_util::unserialize(str, root1);
    if (!ret)
    {
        std::cout << "unserialize failed!!!" << std::endl;
    }
    std::cout << "姓名" << root1["姓名"].asString() << std::endl;
    std::cout << "年龄" << root1["年龄"].asInt() << std::endl;
    int sz = root1["成绩"].size();
    for (int i = 0; i < sz; i++)
    {
        std::cout << "成绩 " << root1["成绩"][i].asFloat() << std::endl;
    }
}

void test_str()
{
    std::string str = ",....,123,234,,,,,,345";
    std::vector<std::string> array;
    string_util::split(str, ",", array);
    std::cout << array.size() << std::endl;
    for (auto s : array)
    {
        DLOG("%s", s.c_str());
    }
}

void file_test()
{
    std::string filename = "./Makefile";
    std::string body;
    file_util::read(filename, body);
    std::cout << body << std::endl;
}

void db_test()
{
    user_table ut(HOST, USER, PASS, DBNAME, PORT);
    Json::Value user;
    user["username"] = "xiaohei";
    user["password"] = "123456";
    bool ret = ut.lose(2);
    // std::string body;
    // json_util::serialize(user,body);
    // std::cout << body <<std::endl;
}

void online_test()
{
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid = 2;
    om.enter_game_hall(uid, conn);
    if (om.is_in_game_hall(uid))
    {
        DLOG(" in game hall");
    }
    else
    {
        DLOG("not in game hall");
    }
    om.exit_game_hall(uid);
    if (om.is_in_game_hall(uid))
    {
        DLOG("in game hall");
    }
    else
    {
        DLOG("not in game hall");
    }
}

int main()
{
    gobang_server _server(HOST, USER, PASS, DBNAME, PORT);
    _server.start(8800);

    // user_table ut(HOST, USER, PASS, DBNAME, PORT);
    // online_manager om;
    // room_manager rm(&ut, &om);
    // room_ptr rp = rm.create_room(10, 20);
    
    // matcher mc(&rm,&ut,&om);
    // room r(10,&ut,&om);
    // online_test();
    return 0;
}